function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
         now = new Date();
         if (now.getTime() > exitTime)
                 return;
    }
}
var cnt = 1;
function drawCurve(points, t) {
     if (points.length == 1) {
         // ctx.fillStyle = "#4F33DD";
         // document.write("<br/>" + "drawing: " + points[0].x + " " + points[0].y);
         // ctx.fillRect(points[0].x, points[0].y, 1, 1);
         var data = new Object();
         data.x = points[0].x;
         data.y = points[0].y;
         data.cnt = cnt;
         this.postMessage(data);
         sleep(1);
    }else {
         newpoints = new Array(points.length - 1);
         for (var i = 0; i < newpoints.length; i++) {
             x = (1 - t) * points[i].x + t * points[i + 1].x;
             y = (1 - t) * points[i].y + t * points[i + 1].y;
             newpoints[i] = new Object();
             newpoints[i].x = x;
             newpoints[i].y = y;
         }
         drawCurve(newpoints, t);
    }
}
 onmessage = function startHere(event) {
     var width = 900, height = 750;
     var n = 7;
     var fir = new Object(), sec = new Object();
     fir.x = Math.random() * width;
     fir.y = Math.random() * height;
     sec.x = Math.random() * width;
     sec.y = Math.random() * height;
     while (true) {
         var points = new Array();
         points[0] = fir;
         points[1] = sec;
         for (var i = 2; i < n; i++) {
             var x = Math.random() * width;
             var y = Math.random() * height;
             points[i] = new Object();
             points[i].x = x;
             points[i].y = y;
         }
         for (var t = 0.0001; t < 1; t += 0.0007) {
             drawCurve(points, t);
         }
         cnt++;
         fir.x = points[n - 1].x; fir.y = points[n - 1].y;
         sec.x = points[n - 2].x; sec.y = points[n - 2].y;
     }
 }
